package ru.dadle8.service.impl.action;

import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.dadle8.service.api.action.ActionExecutor;
import ru.dadle8.service.api.action.ActionService;
import ru.dadle8.service.impl.AbstractExecutorService;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ActionExecutorImpl extends AbstractExecutorService implements ActionExecutor {

    private final Map<String, ActionService> actionServiceMap;

    ActionExecutorImpl(List<ActionService> actionServices) {
        this.actionServiceMap = actionServices.stream()
                .collect(Collectors.toMap(ActionService::getAction, Function.identity()));
    }

    @Override
    public void execute(Update update) throws TelegramApiException {
        Message inMess = update.getMessage();
        Long chatId = inMess.getChatId();

        ActionService actionService = actionServiceMap.get(inMess.getText());
        if (actionService == null) {
            getBot().execute(getMessageHelper().sendMessage(chatId, "Пока такую команду не понимаю"));
        } else {
            actionService.execute(update);
        }
    }
}
